package com.sw.code4j;

import com.sw.code4j.freemarker.FreeMarkerUtil;
import com.sw.code4j.reverse.Reverse;
import com.sw.code4j.vo.ColumnVO;
import com.sw.code4j.vo.ConfigVO;
import com.sw.code4j.vo.TableVO;
import com.gwssi.fusion.datamodel.bo.TDmColumnBO;
import com.gwssi.fusion.datamodel.bo.TDmTableBO;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * @author: 张上亚 2021/11/30 10:32
 * @desc: 填充模板，生成对应的java文件
 */
public class Generator {
    //bo类的模板文件名
    public static final String BO_FTL = "Bo.java.ftl";
    public static final String CONTROLLER_FTL = "Controller.java.ftl";
    public static final String SERVICE_FTL = "Service.java.ftl";


    /**
     * 根据配置生成代码
     *
     * @param config 配置详情
     * @throws Exception
     */
    public static void generate(ConfigVO config) throws Exception {
        Reverse reverse = new Reverse(config);
        List<TDmTableBO> tableBOS = reverse.reverseTable();
        String tablePattern = config.getTable();
        //模糊匹配
        List<TDmTableBO> tables = tableBOS.stream().filter(s -> Pattern.matches(tablePattern, s.getTableName().trim())).collect(Collectors.toList());
        for (TDmTableBO tableBO : tables) {
            TableVO table = new TableVO(tableBO.getTableName(), tableBO.getTableComment());
            List<TDmColumnBO> columnBOS = reverse.reverseColumn(tableBO.getTableName());
            List<ColumnVO> columns = new ArrayList<>();
            for (TDmColumnBO columnBO : columnBOS) {
                ColumnVO columnVO = new ColumnVO(columnBO.getColumnName(), columnBO.getColumnComment(), columnBO.getColumnType(), columnBO.getIsCase());
                columns.add(columnVO);
            }
            Generator.generate(config, table, columns);
        }
    }


    public static void generate(ConfigVO config, TableVO table, List<ColumnVO> columns) throws Exception {
        generateBO(config, table, columns);
        generateController(config, table, columns);
        generateService(config, table, columns);
//        generateJunitTest();
    }

    /**
     * 生成bo类
     *
     * @param config
     * @param table
     * @param columns
     * @return
     * @throws Exception
     */
    public static void generateBO(ConfigVO config, TableVO table, List<ColumnVO> columns) throws Exception {
        Map<String, Object> data = new HashMap<>();
        data.put("config", config);
        data.put("table", table);
        data.put("columns", columns);
        String path = config.getRootFolder() + table.getBoPrefix() + "BO.java";
        FreeMarkerUtil.generateFile(BO_FTL, data, path);
    }

    public static void generateController(ConfigVO config, TableVO table, List<ColumnVO> columns) throws Exception {
        Map<String, Object> data = new HashMap<>();
        data.put("config", config);
        data.put("table", table);
        data.put("columns", columns);
        String path = config.getRootFolder() + table.getControllerPrefix() + "Controller.java";
        FreeMarkerUtil.generateFile(CONTROLLER_FTL, data, path);
    }

    public static void generateService(ConfigVO config, TableVO table, List<ColumnVO> columns) throws Exception {
        Map<String, Object> data = new HashMap<>();
        data.put("config", config);
        data.put("table", table);
        data.put("columns", columns);
        String path = config.getRootFolder() + table.getControllerPrefix() + "Service.java";
        FreeMarkerUtil.generateFile(SERVICE_FTL, data, path);
    }

    public static boolean generateJunitTest() {
        return false;
    }
}
